var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');

// 连接数据库

var conn = mysql.createConnection(models.mysql);
conn.connect();

var jsonWrite = function(res, ret) {
  if(typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    });
  } else {
    res.json(ret);
  }
  
};

// 增加用户接口
router.post('/addUser', (req, res) => {
  var sql = $sql.user.add;
  var params = req.body;
  console.log(params);
  conn.query(sql, [params.username, params.password, params.user_number, params.user_type], function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});
//查询用户接口
router.get('/referUser',(req, res) => {
  var sql = $sql.user.refer;
  conn.query(sql,function(err,result){
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
})
//修改数据接口
router.post('/updUser', (req, res) => {
  var sql = $sql.user.upd;
  var params = req.body;
  conn.query(sql,[params.password, params.user_number, params.id],function(err,result){
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
})

//删除数据接口
router.post('/delUser', (req, res) => {
  var sql = $sql.user.del;
  var params = req.body;
  conn.query(sql,[params.id],function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
})

//车辆表、

// 增加车辆接口
router.post('/addVehicle', (req, res) => {
  var sql = $sql.vehicle.add;
  var params = req.body;
  console.log(params);
  conn.query(sql, [ params.license_plate_number, params.manufacturing_company, params.purchasing_date,
    params.model_of_car,params.the_total,params.fuel_cons,params.basic_maintenance_cost,params.road_toll,params.cumulative_total_cost,
    params.busload,params.carton_numbers,params.load_capacity,params.owner], function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});
//查询车辆接口
router.get('/referVehicle',(req, res) => {
  var sql = $sql.vehicle.refer;
  conn.query(sql,function(err,result){
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
})
//修改车辆数据接口
router.post('/updVehicle', (req, res) => {
  var sql = $sql.vehicle.upd;
  var params = req.body;
  conn.query(sql,[params.license_plate_number, params.manufacturing_company, params.purchasing_date,
    params.model_of_car,params.the_total_mileage,params.fuel_cons,params.basic_maintenance_cost,params.road_toll,params.cumulative_total_cost,
    params.busload,params.carton_numbers,params.load_capacity,params.owner,params.id],function(err,result){
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
})

//删除车辆数据接口
router.post('/delVehicle', (req, res) => {
  var sql = $sql.vehicle.del;
  var params = req.body;
  conn.query(sql,[params.id],function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
})
module.exports = router;


